package com.yitiao.multithread.case01;

/**
 * <h3>线程捕获器</h3>
 * <p>线程内部异常时会回调这个方法</p>
 */
public class ThreadExceptionCatchDemo {

    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("this is test");
                int i = 10 / 0;
            }
        });
        thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            //这里是对Throwable对象进行监控，所以无论是error或者exception都能识别到
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.err.println("thread is " + t.getName());
                System.err.println("exception is " + e.getMessage());
            }
        });
        thread.start();
    }
}
